package cn.stylefeng.guns.modular.gridsystem.timer;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.core.timer.TimerTaskRunner;
import cn.stylefeng.guns.modular.gridsystem.entity.*;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizDeviceStatusAssessParam;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizDeviceExceptionRecordResult;
import cn.stylefeng.guns.modular.gridsystem.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * 状态评估定时任务管理
 */
@Configuration
@EnableScheduling
@RequiredArgsConstructor
@Slf4j
public class DeviceStatueAssessTaskTime {
    private final BizDeviceService bizDeviceService;
    private final BizStationService bizStationService;
    private final BizDeviceStatusAssessService bizDeviceStatusAssessService;

    /**
     * 每周一1点执行一次
     */
    @Scheduled(cron = "0 0 1 ? * MON")
    public void runWeek() {
        DateTime dateTime = DateUtil.lastWeek();
        String startTime = DateUtil.formatDateTime(DateUtil.beginOfWeek(dateTime));
        String endTime = DateUtil.formatDateTime(DateUtil.endOfWeek(dateTime));
        deviceStatusAssess(startTime,endTime,0);
    }

    /**
     * 每月一号1点执行一次
     */
    @Scheduled(cron = "0 0 1 1 * ?")
    public void runMonth() {
        DateTime dateTime = DateUtil.lastMonth();
        String startTime = DateUtil.formatDateTime(DateUtil.beginOfMonth(dateTime));
        String endTime = DateUtil.formatDateTime(DateUtil.endOfMonth(dateTime));
        deviceStatusAssess(startTime,endTime,0);
    }

    /**
     * 每年一月一号1点执行一次
     */
    @Scheduled(cron = "0 0 1 1 1 ?")
    public void runYear() {
        // 获取当前日期
        Date currentDate = new Date();
        // 创建一个日历对象并将其设置为当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        // 从日历中减去一年
        calendar.add(Calendar.YEAR, -1);
        // 将日历设置为去年的年初
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date startOfLastYear = calendar.getTime();
        // 将日历设置为去年的年末
        calendar.set(Calendar.MONTH, Calendar.DECEMBER);
        calendar.set(Calendar.DAY_OF_MONTH, 31);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date endOfLastYear = calendar.getTime();
        String startTime = DateUtil.formatDateTime(DateUtil.beginOfMonth(startOfLastYear));
        String endTime = DateUtil.formatDateTime(DateUtil.endOfMonth(endOfLastYear));
        deviceStatusAssess(startTime,endTime,0);
    }

    public void deviceStatusAssess(String startTime,String endTime,Integer type) {
        List<BizStation> list = bizStationService.list();
        for (BizStation station : list){
            Integer id = station.getId();
            List<BizDeviceExceptionRecordResult> recordResults = bizDeviceService.getDeviceException(id,startTime,endTime);
            for (BizDeviceExceptionRecordResult result:recordResults){
                BizDeviceStatusAssessParam param = new BizDeviceStatusAssessParam();
                param.setDeviceId(result.getDeviceId());
                param.setErrorNumber(result.getUnprocessedRecords());
                param.setNormalNumber(result.getProcessedRecords());
                param.setAssessTime(DateUtil.date());
                param.setType(type);
                bizDeviceStatusAssessService.add(param);
            }
        }
    }

}
